

package com.ncloud.crm.service.impl;

import java.util.List;

import com.ncloud.param.service.SequenceService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.crm.entity.order.Order;
import com.ncloud.crm.entity.order.OrderProject;
import com.ncloud.crm.mapper.OrderProjectMapper;
import com.ncloud.crm.service.BsProjectService;
import com.ncloud.crm.service.OrderProjectService;
import com.ncloud.crm.utils.DateUtils;
import com.ncloud.crm.utils.OrderNodeDefineUtils;
import com.ncloud.crm.utils.TableIdDefineUtils;

@Service
public class OrderProjectServiceImpl implements OrderProjectService {
	
	private static final Logger logger = LogManager.getLogger(OrderProjectServiceImpl.class);
	
	@Autowired
	private OrderProjectMapper orderProjectMapper;
	@Autowired
	private BsProjectService bsProjectService;
	@Reference
	private SequenceService sequenceService;


	public List<OrderProject> getByOrderId(Long orderId) {
		List<OrderProject> orderProjects = orderProjectMapper.getProjectByOrderId(orderId);
		return orderProjects;
	}

	@Override
	public JSONObject getPk(JSONObject json) {
		return null;
	}

	@Override
	public JSONObject validate(JSONObject json,JSONObject result) {
		JSONObject jsonObject = new JSONObject();
		if(json == null){
			throw new UserException(ResultEnum.ORDER_INFO_ERROR);
		}
		if(json.containsKey(OrderNodeDefineUtils.ORDER_PROJECT)){
			jsonObject = json.getJSONObject(OrderNodeDefineUtils.ORDER_PROJECT);
			String modifyTag = jsonObject.getString("modifyTag");
			if(StringUtils.isEmpty(modifyTag)){
				throw new UserException(ResultEnum.PARAM_ERROR);
			}
			/*生成projectId 项目编码*/
			if(!jsonObject.containsKey(TableIdDefineUtils.PROJECT_ID) || StringUtils.isEmpty(jsonObject.getString(TableIdDefineUtils.PROJECT_ID))){
				if (!result.containsKey(TableIdDefineUtils.PROJECT_ID) || StringUtils.isEmpty(result.getString(TableIdDefineUtils.PROJECT_ID))) {
					Long id = sequenceService.getSequence("seq_project_id");
					jsonObject.put(TableIdDefineUtils.PROJECT_ID,id);
					result.put(TableIdDefineUtils.PROJECT_ID,id);
				} else {
					jsonObject.put(TableIdDefineUtils.PROJECT_ID, result.getString(TableIdDefineUtils.PROJECT_ID));
				}

			}
			/*生成orderId*/
			if(!jsonObject.containsKey(TableIdDefineUtils.ORDER_ID) || StringUtils.isEmpty(jsonObject.getString(TableIdDefineUtils.ORDER_ID))) {
				if (!result.containsKey(TableIdDefineUtils.ORDER_ID) || StringUtils.isEmpty(result.getString(TableIdDefineUtils.ORDER_ID))) {
					throw new UserException(ResultEnum.PARAM_ERROR.getCode(), "订单项目信息orderId不存在！");
				} else {
					jsonObject.put(TableIdDefineUtils.ORDER_ID, result.getString(TableIdDefineUtils.ORDER_ID));
				}
			}
			/*处理custId*/
			if(!jsonObject.containsKey(TableIdDefineUtils.CUST_ID) || StringUtils.isEmpty(jsonObject.getString(TableIdDefineUtils.CUST_ID))){
				if(modifyTag.equals("1")){
					if(!result.containsKey(TableIdDefineUtils.CUST_ID) || StringUtils.isEmpty(result.getString(TableIdDefineUtils.CUST_ID))){
						throw new UserException(ResultEnum.PARAM_ERROR.getCode(), "订单项目信息custId不存在！");
					}else{
						jsonObject.put(TableIdDefineUtils.CUST_ID,result.getString(TableIdDefineUtils.CUST_ID));
					}
				}else{
					throw new UserException(ResultEnum.PARAM_ERROR.getCode(),"订单客户信息custId不存在！");
				}
			}
			/*manager*/
			if(!jsonObject.containsKey("manager") || StringUtils.isEmpty(jsonObject.getString("manager"))){
				jsonObject.put("manager",json.getLong("userId"));
			}

			/*test_flag*/
			if(!jsonObject.containsKey("testFlag") || StringUtils.isEmpty(jsonObject.getString("testFlag"))){
				jsonObject.put("testFlag","0");//设置不是测试项目
			}
			//设置人员
			jsonObject.put("createUser",json.getLong("userId"));

			//设置创建时间
			jsonObject.put("createTime", result.getString("createTime")!=null?result.getString("createTime"): DateUtils.getDateTime());

		}
		return jsonObject;
	}

	@Override
	public JSONObject insert(JSONObject json) {

		OrderProject orderProject = JSON.parseObject(json.toJSONString(),OrderProject.class);
		try {
			validation(orderProject);
			orderProjectMapper.insertOrderProject(orderProject);
		}catch (RuntimeException e){
			e.printStackTrace();
			throw new UserException(ResultEnum.DATABASE_ERROR);
		}
		return json;
	}
	private void validation(OrderProject account){

	}

	@Override
	public OrderProject getNewProjectInfo(Order order) {
		OrderProject orderProject = new OrderProject(); 
//		List<OrderProject> ordreProjects = this.getByOrderId(order.getOrderId());
//		ordreProjects.removeIf(x->Constant.DEL.equals(x.getModifyTag()));
//		
//		if(ordreProjects.isEmpty()){
//			BsProject project = bsProjectService.getProjectInfo(order.getProjectId());
//			BeanUtils.copyProperties(project, orderProject);
//		}else{
//			orderProject = ordreProjects.get(0);
//		}
		
		return orderProject;
	}
}

